package com.ycfmedical.bed.utils;

import static com.ycfmedical.bed.ControlBedConstants.CONTROL_USER_NONE;

import com.tencent.bugly.imsdk.crashreport.crash.jni.NativeExceptionHandler;
import com.ycfmedical.bed.Constants;
import com.ycfmedical.bed.ControlBedConstants;
import com.ycfmedical.bed.R;
import com.ycfmedical.bed.bean.BedParameterInfo;
import com.ycfmedical.bed.bean.BodySignParameterInfo;
import com.ycfmedical.bed.bean.ImUser;
import com.ycfmedical.bed.bean.NurseControl;
import com.ycfmedical.bed.factory.OrderFactory;
import com.ycfmedical.bed.listener.IvsModel;
import com.ycfmedical.bed.local.DataManager;
import com.ycfmedical.bed.local.dao.UserDao;
import com.ycfmedical.bed.local.entity.User;
import com.ycfmedical.bed.model.VsModel;
import com.ycfmedical.bed.serverdata.DBUtil;
import com.ycfmedical.bed.ui.ControlActivity;
import com.ycfmedical.bed.ui.ImActivity;
import com.ycfmedical.bed.ui.SplashActivity;
import com.ycfmedical.bed.ui.fragment.BedControlFragment;
import com.ycfmedical.bed.ui.fragment.ControlCrpFragment;
import com.ycfmedical.bed.ui.fragment.ControlnNurseFragment;
import com.ycfmedical.bed.wifi.WifiEvent;

import java.util.List;

import cn.droidlover.xdroidmvp.event.BusProvider;
import cn.droidlover.xdroidmvp.kit.Kits;
import cn.droidlover.xdroidmvp.log.XLog;
import cn.droidlover.xdroidmvp.mvp.XManager;


/**
 * Created by Administrator on 2017-04-06.
 * 登录登出工具类
 */

public class OrderMsgExecute {

    WifiEvent wifi = WifiEvent.getInstance();

    public static void orderMsgScreen(String orderMsg)
    {
//        XLog.e("orderMsg","orderMsg = "+orderMsg);
        String medicalCard = DataManager.getInstance().getUser().getMedicalCard();
        String bedId = Constants.bedId;
        boolean linkStatus = true;
        //判断卡号
        if (medicalCard == null)
        {
            medicalCard = Constants.medicalCard;
        }
        if (orderMsg.length() == 46)
        {
            //指令号判断
            String OrderIdentity = orderMsg.substring(8,12);
            String nurseOrderBedId = orderMsg.substring(6, 8);
            //判断是否是当前床的消息
            if (Constants.bedId.equals(nurseOrderBedId))
            {
//                //连接成功(判断底层已经启动)
//                if(orderMsg.equals(OrderFactory.getMessage(ControlBedConstants.RECEIVE_ZGB_MACADDRESS)))
//                {
//                    linkStatus = true;
//                }

                //护理体位加解锁
                if (OrderIdentity.equals("0006"))
                {
                    //加解锁床号（暂无任何意义）
                    String lockNum = orderMsg.substring(16,18);
                    //加解锁类型
                    String lockState = orderMsg.substring(14,16);
                    if(lockState.equals("C2"))
                    {
                        BusProvider.getBus().postSticky(new NurseControl(2));
                    }
                    else if (lockState.equals("C8"))
                    {
                        BusProvider.getBus().postSticky(new NurseControl(1));
                    }
                }

                //接收ZGB生命体征参数
                else if (OrderIdentity.equals("0021"))
                {
                    String HeartRate = orderMsg.substring(12,14);//心率
                    String breathe = orderMsg.substring(14,16);  //呼吸
                    String bodyState = orderMsg.substring(26,28);//床状态
                    String bedState = orderMsg.substring(28,32); //传感器
                    BusProvider.getBus().postSticky(new BodySignParameterInfo(orderMsg,HeartRate,breathe,bodyState,bedState));

                }

                //接收床体当前角度值
                else if(OrderIdentity.equals("0022")||OrderIdentity.equals("A022"))
                {
                    String angleOrderMsg = orderMsg;
                    //床体高度
                    int nurseStature = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(12,14));
                    //倾斜角度
                    int slantAngle = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(16,18));
                    //俯仰角1
                    int pitchAngleOne = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(18,20));
                    //横滚角1
                    int rollAngleOne = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(22,24));
                    //航向角1
                    int courseAngleOne = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(26,28));
                    //俯仰角2
                    int pitchAngleTwo = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(30,32));
                    //横滚角2
                    int rollAngleTwo = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(34,36));
                    //航向角2
                    int courseAngleTwo = ConvertOrderCheckoutUtil.StrC16T10(orderMsg.substring(38,40));

                    BusProvider.getBus().postSticky(new BedParameterInfo(angleOrderMsg,slantAngle,nurseStature,pitchAngleOne
                            ,rollAngleOne,courseAngleOne,pitchAngleTwo,rollAngleTwo,courseAngleTwo));
                }

                //接收到床体当前睡姿数据
                else if (OrderIdentity.equals("0023"))
                {

                }

                //接收到床体当前环境数据
                else if (OrderIdentity.equals("0024"))
                {

                }

                //护士站通知床登录
                else if (OrderIdentity.equals("0027"))
                {
//                    login(orderMsg);
                }

                //护士站通知床登出
                else if (OrderIdentity.equals("0028"))
                {
//                    logOut();
                }
                //传递当前床体权限状态给护士站
                else if (OrderIdentity.equals("0029"))
                {
                    WifiEvent wifi = WifiEvent.getInstance();
                    wifi.senddata(Constants.fhHeader+Constants.nurseId+"0030"+Constants.lockState+"0000000000000000000000000000"+Constants.bedId+"00");
                }
                //接收到CPR动作完成指令停止动画
                else if (OrderIdentity.equals("A040"))
                {
                    ControlCrpFragment crpFragment = null;
                    try
                    {
                        crpFragment = BedControlFragment.getControlCrpFragment();
                        crpFragment.stopGif();
                    }
                    catch (Exception e)
                    {
                        e.printStackTrace();
                    }
                }
                //硬件报错指令
                else if (OrderIdentity.equals("00A5"))
                {
                    //目标对象判断
                    String  wrongMac = orderMsg.substring(8, 10);
                    switch (wrongMac)
                    {
                        case "09":
                            //背板传感器
                            break;
                        case "1A":
                            //腿板传感器
                            break;
                        case "2D":
                            //整床角度传感器
                            break;
                        case "3C":
                            //高度传感器
                            break;
                        case "4E":
                            //体征床垫
                            break;
                    }
                }
                //接收断网状态下回传的睡眠数据
                else if (OrderIdentity.equals("00AA"))
                {

                }
            }
        }



    }


    //登录操作
    private static void login(String medicalCard) {
        XManager manager = XManager.getInstance();
        manager.exit();
        loadData(medicalCard);
    }

    //登出操作
    private static void logOut() {

        ImActivity.imLogout();
        XManager manager = XManager.getInstance();
        manager.exit();
        //清空数据

    }

    // 从数据库获取数据
    public static void loadData(String medicalCard)
    {
        List<User> userList = UserDao.getAllUserInfoList();
        if (!Kits.Empty.check(userList)) {
            for (User user : userList) {
                if (user.getMedicalCard().equals(medicalCard))
                {
                    DataManager.getInstance().setUser(user);
                    ImUser.getInstance().setImAccount(user.getImAccount());
                    BusProvider.getBus().post(user);
                }
            }
        }
    }

}
